local test = UnitTest:New("07DongleSlash")
local Dongle = DongleStub("Dongle-1.0")

function test:dongle_initialize_slash()
	assert_fails(function() Dongle.InitializeSlashCommand({},"beta","beta help") end, "You must call 'InitializeSlashCommand' from a registered Dongle.")

	local beta = Dongle:New("beta")
	assert_fails(function() beta:InitializeSlashCommand() end, argerror(2))
	assert_fails(function() beta:InitializeSlashCommand(1) end, argerror(2))
	assert_fails(function() beta:InitializeSlashCommand("beta help") end, argerror(3))
	assert_fails(function() beta:InitializeSlashCommand("beta help", 1) end, argerror(3))
	assert_fails(function() beta:InitializeSlashCommand("beta help", "BETA") end, argerror(4))
	assert_fails(function() beta:InitializeSlashCommand("beta help", "BETA", 1) end, argerror(4))	

	local cmd = beta:InitializeSlashCommand("beta help", "BETA", "beta")
	assert(type(cmd) == "table")
end

function test:dongle_register_slash_handler()
	assert_fails(function() Dongle.RegisterSlashHandler() end, "You must call 'RegisterSlashHandler' from a Dongle slash command object.")
	local gamma = Dongle:New("gamma")
	local cmd = gamma:InitializeSlashCommand("gamma help", "GAMMA", "gamma")

	assert_fails(function() cmd:RegisterSlashHandler() end, argerror(2))
	assert_fails(function() cmd:RegisterSlashHandler(1) end, argerror(2))
	assert_fails(function() cmd:RegisterSlashHandler("gamma help") end, argerror(3))
	assert_fails(function() cmd:RegisterSlashHandler("gamma help", 1) end, argerror(3))
	assert_fails(function() cmd:RegisterSlashHandler("gamma help", "gamma") end, argerror(4))
	assert_fails(function() cmd:RegisterSlashHandler("gamma help", "gamma", 1) end, argerror(4))

	cmd:RegisterSlashHandler("cmd2 help", "cmd2 pattern", "GammaFunc")
	cmd:RegisterSlashHandler("cmd3 help", "cmd3 pattern", function() end)
end

function test:dongle_call_handler()
	local delta = Dongle:New("delta")
	local cmd = delta:InitializeSlashCommand("delta help", "DELTA", "delta")

	local obj
	cmd:RegisterSlashHandler("cmd1 help", "cmd1", function() obj = delta end)
	assert(not obj)
	_G.SlashCmdList.DELTA("cmd1")
	assert(obj == delta)

	obj = nil
	cmd:RegisterSlashHandler("cmd2 help", "cmd2", "cmd2")
	function delta:cmd2() obj = self end
	assert(not obj)
	_G.SlashCmdList.DELTA("cmd2")
	assert(obj == delta)
end

function test:dongle_call_handler_parse()
	local epsilon = Dongle:New("epsilon")
	local cmd = epsilon:InitializeSlashCommand("epsilon help", "EPSILON", "epsilon")
	
	local obj, int
	cmd:RegisterSlashHandler("cmd1 help", "cmd1 (%d+)", function(i) int = i end)
	assert(not int)
	assert(not obj)
	_G.SlashCmdList.EPSILON("cmd1")
	assert(not int)
	assert(not obj)
	_G.SlashCmdList.EPSILON("cmd1 blarg")
	assert(not int)
	assert(not obj)
	_G.SlashCmdList.EPSILON("cmd1 12345")
	assert(int == "12345")
end

function test:dongle_print_usage()
	assert_fails(function() Dongle.PrintUsage() end, "You must call 'PrintUsage' from a Dongle slash command object.")
end

test:Run()
